package com.ailot.cloud.upms.api.service.impl;

import cn.hutool.core.util.StrUtil;
import com.ailot.cloud.upms.api.mapper.SysRoleMenuMapper;
import com.ailot.cloud.upms.api.service.SysRoleMenuService;
import com.ailot.cloud.upms.common.entity.SysRoleMenu;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色菜单
 */
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements SysRoleMenuService {


    @Override
    public Boolean updateRoleMenus(String roleId, String menuIds) {
        // 删除之前的角色菜单关系
        this.remove(Wrappers.<SysRoleMenu>query().lambda()
                .eq(SysRoleMenu::getRoleId, roleId));

        if (StrUtil.isBlank(menuIds)) {
            return Boolean.TRUE;
        }
        List<SysRoleMenu> roleMenuList = Arrays
                .stream(menuIds.split(","))
                .map(menuId -> new SysRoleMenu(roleId, menuId)).collect(Collectors.toList());
        return this.saveBatch(roleMenuList);
    }
}
